package com.sinaapp.kltq.dinosaurweather;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.sinaapp.kltq.data.CityListItem;
import com.sinaapp.kltq.weathermanager.WeatherManager;

import java.util.List;


public class BeginningActivity extends ActionBarActivity {

    private WeatherManager wm;

    private Handler myHandler = new Handler() {
        public void handleMessage(Message msg) {
            Intent intent;
            switch (msg.what) {
                case 3:
                    // 空城市列表
                    intent = new Intent(BeginningActivity.this, EmptyActivity.class);
                    startActivity(intent);
                    finish();
                    break;
                case 2:
                    // 网络错误
                    Toast.makeText(getApplicationContext(), "网络错误", Toast.LENGTH_SHORT).show();
                    intent = new Intent(BeginningActivity.this, FlipWhiteActivity.class);
                    intent.putExtra("cityIndex", 0);
                    intent.putExtra("pageType", 'r');
                    startActivity(intent);
                    finish();
                    break;
                case 1:
                    // 网络正常
                    intent = new Intent(BeginningActivity.this, FlipWhiteActivity.class);
                    intent.putExtra("cityIndex", 0);
                    intent.putExtra("pageType", 'r');
                    startActivity(intent);
                    finish();
                    break;
            }
            super.handleMessage(msg);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_beginning);

        wm = new WeatherManager(getApplicationContext());

        new Handler().postDelayed(new Runnable() {
            public void run() {

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Message message = new Message();
                        message.what = 1;
                        List<CityListItem> list = wm.getCityList();
                        if (list.size() == 0) {
                            message.what = 3;
                        } else {
                            for (CityListItem item : list) {
                                message.what = wm.pleaseUpdateThisSir(item.city) ? message.what : 2;
                            }
                        }
                        myHandler.sendMessage(message);
                    }
                }).start();
            }
        }, 900);

        if(!isServiceRunning(this,"NoticeService")) {


            System.out.println("isServiceRunning()");
            Intent intent = new Intent(BeginningActivity.this, NoticeService.class);
            //intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            BeginningActivity.this.startService(intent);
        }
    }
    //查看后台服务是否启动
    public static boolean isServiceRunning(Context mContext,String className) {
        boolean IsRunning = false;
        ActivityManager activityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> serviceList   = activityManager.getRunningServices(30);
        if (!(serviceList.size()>0)) {
            return false;
        }
        for (int i=0; i<serviceList.size(); i++) {
            if (serviceList.get(i).service.getClassName().equals(className) == true) {
                IsRunning = true;
                break;
            }
        }
        return IsRunning ;
    }
}
